/*********************************************************************************
 *      Copyright:  (C) 2022 lijiale<244871365@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  nbiot.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(20/01/22)
 *         Author:  lijiale<244871365@qq.com>
 *      ChangeLog:  1, Release initial version on "20/01/22 09:57:48"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>

#include "comport.h"
#include "atcmd.h"
#include "bc28_at.h"
#include "nbiot.h"

#define  PATH "/dev"
#define  BUF_MAX    512
#define  DEV_NAME   64
#define  TIME_OUT   300

int find_nbiot_dev( comport_t *comport)//查找BC28对应的串口
{
    DIR             *pDir = NULL;
    struct dirent   *pEnt = NULL;
    int             i = 0;
    int             rv = 0;
    char            buf[BUF_MAX];
    char            devname[BUF_MAX];

    memset(buf, 0, sizeof(buf));
    memset(devname, 0, sizeof(devname));

    pDir = opendir(PATH);
    if( NULL == pDir )
    {
        printf("open dir failure");
        return -1;
    }

    while( pEnt = readdir(pDir) )
    {
        if( strstr(pEnt->d_name,"ttyUSB") )
        {
            snprintf(devname, sizeof(devname), "%s/%s", PATH, pEnt->d_name);
            if( comport_open( comport, devname ) != 0 )
            {
                continue;
            }
            printf("open is %s\n",devname);

            if( comport_conf(comport) != 0 )
            {
                printf("config failure\n");
                continue;
            }

            rv = send_atcmd(comport, AT_MODEL, buf, sizeof(buf), TIME_OUT);
            if( rv < 0 )
            {
                comport_close(comport);
                continue;
            }
            else
            {
                if( strstr(buf,comport->modelname) )
                {
                    strncpy(comport->tty_name,devname,sizeof(comport->tty_name));
                                       
                    closedir(pDir);
                    
                    return 0;
                }
            }

            comport_close(comport);
        }
    }
    closedir(pDir);

    return -2;
}

#if 0
int set_nbiot_cfun(comport_t *comport,nbiot_hwinfo_t *nbiot)
{
    int                 rv;

    rv = nbiot_module(comport,nbiot);
    if( rv < 0 )
    {
        printf("nbiot_module is failure. rv :%d..\n",rv);
        return -1;
    }

    return 0;
}
#endif

int check_nbiot_register(comport_t *comport)//查注册信息
{
    int                 rv;

    rv = atcmd_cereg(comport);
    if( rv < 0 )
    {
        printf("atcmd cereg is failure...\n");
        return -1;
    }

    return 0;
}

int set_nbiot_register( comport_t *comport , char *ip , char *port)//设置注册信息(ip,端口)
{
    int                 rv;

    rv = atcmd_ncdp(comport, ip, port);
    if( rv < 0 )
    {
        printf("atcmd ncdp is failure ...\n");
        return -1;
    }

    return 0;
}

int nbiot_module( comport_t* comport , nbiot_hwinfo_t  *nbiot )
{
    int                 rv;

    rv = atcmd_cgsn(comport, nbiot->imei, sizeof(nbiot->imei) );//查IMEI号
    if( rv < 0 )
    {
        printf("at+cgsn=1 failure\n");
        return -1;
    }

    rv = atcmd_cimi(comport, nbiot->cimi, sizeof(nbiot->cimi) );//查IMSI号
    if( rv < 0 )
    {
        printf("at+cimi failure\n");
        return -2;
    }

    rv = atcmd_ati(comport,nbiot);//查制造制造商、版本序列号
    if( rv < 0 )
    {
        printf("at+ati failure\n");
        return -3;
    }

    rv = atcmd_cgmm(comport,nbiot->modules_name , sizeof(nbiot->modules_name));//模块名
    if(rv < 0)
    {
        printf("at+cgmm is failure\n");
        return -4;
    }

    rv = atcmd_cgmr(comport , nbiot->version , sizeof(nbiot->version));//版本信息
    if( rv < 0 )
    {
        printf("at+cgmr is failure \n");
        return -5;
    }

    return 0;
}

int check_nbiot_attach(comport_t *comport, nbiot_hwinfo_t *nbiot)
{
	int                 rv; 

    rv = atcmd_at(comport);
    if( rv < 0 ) 
    {   
        printf("nbiot_at is failure  rv  :%d\n",rv);
        return -1; 
    }
	
	rv = nbiot_module(comport, nbiot);
    if( rv < 0 ) 
    {   
        printf("nbiot_module is failed..rv :%d\n",rv);
        return -2; 
    }

    rv = atcmd_csq(comport);
    if( rv < 0 ) 
    {   
        printf("nbiot_csq is failure rv  :%d\n",rv);
        return -3; 
    }

    rv = atcmd_cgatt1(comport);
    if( rv < 0 ) 
    {   
        printf("nbiot_cgatt1? is failure  rv :%d\n" , rv);
        return -4; 
    }
    
    rv = atcmd_cereg(comport);
    if( rv < 0 ) 
    {   
        printf("nbiot_cereg is failure rv :%d\n" , rv);
        return -5;    
    }
	
	rv = atcmd_cgatt(comport);
    if( rv < 0 )
    {
        printf(" nbiot_cgatt is  failure  rv :%d\n", rv);
        return -6;
    }
	
    rv = atcmd_cgpaddr(comport);
    if( rv < 0 )
    {
        printf(" nbiot_cgpaddr id failure  rv :%d\n");
        return -7;
    }

    return 0;
}

int nbiot_report_cloud(comport_t *comport, int temp)//上报数据
{
    int                 rv;

    rv = atcmd_qlwuladtaex(comport , temp);
    if( rv < 0 )
    {
        printf("atcmd_qlwuladteax is failure rv :%d...\n",rv);
        return -1;
    }

    return 0;
}
